import 'package:bxultimate/app/pages/application/organization/organization_statistics_template_create/widgets/radio_from.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateCreateDto.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/app/app_input.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart' hide TDInput, TDInputType;

import 'controller.dart';

class OrganizationStatisticsTaskCreatePage extends StatefulWidget {
  const OrganizationStatisticsTaskCreatePage({Key? key}) : super(key: key);

  @override
  State<OrganizationStatisticsTaskCreatePage> createState() => _OrganizationStatisticsTaskCreatePageState();
}

class _OrganizationStatisticsTaskCreatePageState extends State<OrganizationStatisticsTaskCreatePage> {
  final controller = Get.find<OrganizationStatisticsTaskCreateController>();
  final state = Get.find<OrganizationStatisticsTaskCreateController>().state;

  var selectList = ['文本', '数字', '日期', '单选项'];

  _buildText(type) {
    return Column(
      children: [
        TDInput(
          controller: controller.placeholderController,
          leftLabel: '标题',
          required: true,
          type: TDInputType.special,
          hintText: '请输入标题',
          textAlign: TextAlign.end,
          backgroundColor: Colors.transparent,
        ),
        TDInput(
          controller: controller.placeholderTwoController,
          leftLabel: '提示文字',
          hintText: '请输入提示文字',
          type: TDInputType.special,
          textAlign: TextAlign.end,
          backgroundColor: Colors.transparent,
        ),
        Obx(() => TDCell(
          title: '必填',
          rightIconWidget: TDSwitch(
              isOn: state.requiredSelected,
              onChanged: controller.requiredChange),
        )),
        Gaps.vGap12,
        TDButton(
          text: '确认',
          theme: TDButtonTheme.primary,
          isBlock: true,
          onTap: () {
            controller.addFromItem(type);
          },
        )
      ],
    );
  }

  _buildDate() {
    return Column(
      children: [
        TDInput(
          controller: controller.placeholderController,
          leftLabel: '标题',
          required: true,
          type: TDInputType.special,
          hintText: '请输入标题',
          textAlign: TextAlign.end,
          backgroundColor: Colors.transparent,
        ),
        TDInput(
          controller: controller.placeholderTwoController,
          leftLabel: '提示文字',
          hintText: '请输入提示文字',
          type: TDInputType.special,
          textAlign: TextAlign.end,
          backgroundColor: Colors.transparent,
        ),
        TDCell(
          title: '日期类型',
          arrow: false,
        ),
        TDRadioGroup(
          onRadioGroupChange: controller.dateChange,
          selectId: controller.radioList[0].value,
          child: ListView.builder(
            padding: EdgeInsets.all(0),
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              return TDRadio(
                id: controller.radioList[index].value,
                title: controller.radioList[index].label,
                size: TDCheckBoxSize.large,
              );
            },
            itemCount: controller.radioList.length,
          ),
        ),
        Obx(() => TDCell(
          title: '必填',
          rightIconWidget: TDSwitch(
              isOn: state.requiredSelected,
              onChanged: controller.requiredChange),
        )),
        Gaps.vGap12,
        TDButton(
          text: '确认',
          theme: TDButtonTheme.primary,
          isBlock: true,
          onTap: () {
            controller.addFromItem(2);
          },
        )
      ],
    );
  }

  _buildRadio(dto) {
    return RadioForm(addFromItem: controller.addRadioForm, dto: OrganizationAnnouncementTemplateCreateDto(),);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OrganizationStatisticsTaskCreateController>(
        init: OrganizationStatisticsTaskCreateController(),
        builder: (_) => MkPage(
            child: Scaffold(
              backgroundColor: Colors.transparent,
              appBar: TDNavBar(
                backgroundColor: Colors.transparent,
                title: '创建任务',
              ),
              body: MkScrollView(
                  bottomButton: Container(
                    width: double.infinity,
                    padding: EdgeInsets.only(
                      left: AppTheme.iDefaultPadding,
                      right: 0,
                      top: AppTheme.iDefaultPadding/2,
                      bottom: AppTheme.iDefaultPadding/2,
                      // right: AppTheme.iDefaultPadding,
                    ),
                    margin: EdgeInsets.all(0),
                    color: Colors.white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 1,
                          child: GestureDetector(
                            onTap: controller.templateSelect,
                            child: Container(
                              child: Column(
                                children: [
                                  Icon(Icons.access_time_filled_sharp,size: 16,),
                                  Text('模板')
                                ],
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 4,
                          child: TDButton(
                            text: '确认创建',
                            theme: TDButtonTheme.primary,
                            isBlock: true,
                            onTap: controller.create,
                          ),
                        )
                      ],
                    ),
                  ),
                  children: [
                    TDInput(
                      controller: controller.titleController,
                      leftLabel: '任务名称',
                      required: true,
                      type: TDInputType.special,
                      textAlign: TextAlign.end,
                      backgroundColor: Colors.transparent,
                    ),
                    TDInput(
                      controller: controller.descriptionController,
                      leftLabel: '描述',
                      required: true,
                      type: TDInputType.special,
                      textAlign: TextAlign.end,
                      backgroundColor: Colors.transparent,
                    ),
                    Obx(() => TDCell(
                      style: TDCellStyle(
                          backgroundColor: Colors.transparent,
                          requiredStyle: TextStyle(
                            color: TDTheme.of(context).errorColor6,
                            fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                            height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                            fontWeight: FontWeight.w400,
                          )),
                      arrow: true,
                      required: true,
                      noteWidget: Text(
                        '已选择${state.idcardList.length}人',
                        style: TextStyle(fontSize: 16),
                      ),
                      title: '发送到',
                      onClick: controller.toSelectUser,
                    )),
                    Gaps.vGap8,
                    Padding(
                        padding: EdgeInsets.only(
                            left: AppTheme.iDefaultPadding,
                            right: AppTheme.iDefaultPadding),
                        child: Column(children: [
                          AppTitle(title: '表单项'),
                        ])),
                    Gaps.vGap8,
                    Obx(() => state.formItem.length == 0
                        ? SizedBox()
                        : Column(
                      children: [
                        for (var i = 0; i < state.formItem.length; i++)
                          Slidable(
                            endActionPane: ActionPane(
                              motion: ScrollMotion(),
                              children: [
                                SlidableAction(
                                  // An action can be bigger than the others.
                                  onPressed: (context) {
                                    editFormItem(i);
                                  },
                                  backgroundColor: Colors.blue,
                                  foregroundColor: Colors.white,
                                  icon: Icons.edit,
                                  label: '编辑',
                                ),
                                SlidableAction(
                                  // An action can be bigger than the others.
                                  onPressed: (context) {
                                    controller.removeFormItem(i);
                                  },
                                  backgroundColor: Colors.redAccent,
                                  foregroundColor: Colors.white,
                                  icon: Icons.delete,
                                  label: '删除',
                                ),
                              ],
                            ),
                            child: TDCell(
                              title: '${state.formItem[i].placeholder}',
                              required: state.formItem[i].required,
                              rightIconWidget: Row(
                                children: [
                                  state.formItem[i].type == 1
                                      ? TDTag(
                                    '文本',
                                    theme: TDTagTheme.primary,
                                    isOutline: true,
                                  )
                                      : SizedBox(),
                                  state.formItem[i].type == 2
                                      ? TDTag(
                                    '数字',
                                    theme: TDTagTheme.primary,
                                    isOutline: true,
                                  )
                                      : SizedBox(),
                                  state.formItem[i].type == 3
                                      ? TDTag(
                                    '日期',
                                    theme: TDTagTheme.primary,
                                    isOutline: true,
                                  )
                                      : SizedBox(),
                                  state.formItem[i].type == 4
                                      ? TDTag(
                                    '单选项',
                                    theme: TDTagTheme.primary,
                                    isOutline: true,
                                  )
                                      : SizedBox(),
                                ],
                              ),
                              description:
                              state.formItem[i].placeholderTwo,
                            ),
                          )
                      ],
                    )),
                    Gaps.vGap8,
                    TDButton(
                      text: '添加表单项',
                      isBlock: true,
                      size: TDButtonSize.small,
                      theme: TDButtonTheme.light,
                      onTap: () {
                        controller.placeholderController.text = '';
                        controller.placeholderTwoController.text = '';
                        controller.currentEditIndex=-1;
                        TDPicker.showMultiPicker(context, title: '选择表单类型',
                            onConfirm: (selected) {
                              Navigator.of(context).pop();
                              var height = 340.0;
                              var child = _buildText(selected[0]);
                              if (selected[0] == 2) {
                                // 日期
                                child = _buildDate();
                                height = 560;
                              }
                              if (selected[0] == 3) {
                                // 日期
                                child = _buildRadio(null);
                                height = 700;
                              }

                              UtilMk.openBottomSheet(
                                  height, selectList[selected[0]], child);
                            }, data: [selectList]);
                      },
                    ),
                  ]),
            )));
  }

  @override
  void dispose() {
    Get.delete<OrganizationStatisticsTaskCreateController>();
    super.dispose();
  }

  void editFormItem(int i) {
    var height = 340.0;
    num type = state.formItem[i].type! - 1;
    var child = _buildText(type);
    controller.placeholderController.text = state.formItem[i].placeholder!;
    controller.placeholderTwoController.text = state.formItem[i].placeholderTwo!;
    state.requiredSelected = state.formItem[i].required!;

    controller.currentEditIndex = i;
    if (type == 2) {
      // 日期
      child = _buildDate();
      height = 560;
    }
    if (type == 3) {
      // 日期
      child = _buildRadio(state.formItem[i]);
      height = 700;
    }
    UtilMk.openBottomSheet(height, selectList[int.parse(type.toString())], child);
  }
}